1.0.53 • Published 1 year ago

helium-sdx v1.0.53

Weekly downloads
219
License
ISC
Repository
gitlab
Last release
1 year ago

helium-sdx

SDx is short for "Source Derivation," and it's a relationship between "source" variables and "deriver" functions. Deriver functions run every time their source dependencies update.

import {Source, derive} from "helium-sdx";

const source = new Source(1);

derive(() => console.log(source.get())); // outputs "1";

source.set(2): // outputs "2";

Website: helium-ui.tech


Quick Reference Example (QRE)

Here's most of the general functionality

import { 
  Source, derive, Sourcify, SourceArray, 
  SourceMap, deriverSwitch, DerivationManager, 
  awaitSource, DdxSource 
} from "helium-sdx";

const source = new Source(1);
const sourceArray = new SourceArray([1, 2, 3]);
const sourceMap = new SourceMap([["a", 1], ["b", 2], ["c", 3]]);
const state = Sourcify({
  a: 1,
  b: {
    c: 3,
    d: [1, 2, 3]
  }
});

// will run any time source, sourceArray[2], or sourceMap["c"] change
derive(() => {
  console.log(
    source.get() 
    + sourceArray.get(2)
    + sourceMap.get("c")
  )
});

// verbose derivation args
derive({
  // if given an anchor, deriver will only run when that anchor allows it
  anchor: "NO_ANCHOR",
  // how long to allow for sources to change before responding
  batching: "fiveSec",
  // will run again if a source is updated to have the same value as it already has
  allowEqualValueUpdates: true,
  // performance hack for derivers with no conditionally accessed sources
  dependenciesNeverChange: true,
  fn: () => {
    // this deriver will now output debug data
    DerivationManager.debugDeriver(); 
    console.log(state.a + state.b.c);
  }
});


// switch will only output "even" or "odd" when it switches from one to the other
deriverSwitch({
  watch: () => (source.get() + state.a) % 2,
  responses: [
    { match: 0, action: () => console.log("even")},
    { test: /1/, action: () => "TRY_NEXT_ACTION"},
    { match: 1, action: () => console.log("odd")}
  ]
});


// similar to above with a "DdxSource" instead
const ddxSource = new DdxSource(
  () => (source.get() + state.a) % 2
);
derive(() => 
  console.log(ddxSource.get() === 0 ? "even" : "odd")
)


// a way to handle async in SDx
derive(() => {
  const asyncVal = awaitSource("someId", () => 
    new Promise<number>((res) => setTimeout(res, 1500))
  );
  // first run
  if (asyncVal === undefined) {
    console.log("Waiting for promise to resolve");
  } else {
    // 1.5 seconds later
    console.log(asyncVal);
  }
})

For web app stuff, checkout helium-ui

P.S. I hope something good happens to you today

1.0.37

2 years ago

1.0.36

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.53

1 year ago

1.0.52

1 year ago

1.0.35

3 years ago

1.0.33

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.30

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.27

3 years ago

1.0.24

3 years ago

1.0.22

3 years ago

1.0.23

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.2

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago