パッケージの詳細

@submodule/core

submodule-js1kMIT12.5.1

Structural way to build node and deno application

ioc, configuration, structure, composition

readme

Submodule documentation

The doc is out of date, soon to be updated

actualization in the document meant the result of a function excution, assume the function execution is expensive/or unexpected (for example, we expect limited amount of instances)

factory is the function that used to provide the value

Common usecases

Provide a value factory, actualize when needed. Use provide

const scope = createScope()

const valueExecutor = provide(() => /* Some value */)
const value = await scope.resolve(valueExecutor)

Factory can be a sync or async function. scope#resolve is always async ops Within same the same scope, the value is cached (singleton)

Derive value

const scope = createScope()

const seed = provide(async () => /* Read from config */)
const hashFnExecutor = map(seed, (seed) => () => /* Do hashing */)
const hashFn = await scope.resolve(hashFnExecutor)

Create submodule function that requires some input

Given this code

map(
  dependencies,                     // static dependency reference
  (dependencies) => {               // actualized static dependencies
                                    // preparation code, reusable part, cache etc
    return (inputParams: any[]): FinalValue => { // Runtime dependencies
      /* Implementation */
    }
  }
)

Example

const scope = createScope()

const seed = provide(async () => /* Read from config */)
const hashFnExecutor = map(seed, (seed) => (value: string) => /* Do hashing */)
const hashFn = await scope.resolve(hashFnExecutor)
// hashFn('value') <-- hashed value

Use multiple dependencies

Use combine to group multiple dependencies

const stringValue = provide(() => '1')
const intValue = provide(() => 2)
const combined: Executor<{ stringValue: string, intValue: number }> = combine({ stringValue, intValue })

const use = map(combined, ({ intValue, stringValue}) => { /**/ })
//                           ^^ int
//                                     ^^ string

// shortcut, works most of the time, soemtime typescript can't resolve it
const use = map({ stringValue, intValue }, ({ intValue, stringValue}) => { /**/ })

Group similar executors (like routes)

const stringValue1 = provide(() => '1')
const stringValue2 = provide(() => '2')

const stringValues: Executor<string[]> = group(stringValue1, stringValue2 )

Refer to the current scope inside a factory / conditional value

Use the special scoper to access to the current scope.

const constantSeed = provide(() => 1)
const randomSeed = provide(() => Math.random())

const seed = map(
  combine({
    scoper,
    constantSeed: value(constantSeed), // wrap inside value so it won't be resolved
    randomSeed: value(randomSeed), // wrap inside value so it won't be resolved
  }),
  async ({ 
    scoper,
    constantSeed,
    randomSeed
  }) => {
    if (condition) return await scoper.resolve(constantSeed)
    return await scoper.resolve(randomSeed)
  }
)

Can also use flat to resolve Executor<Executor<V>>

// In that case 
const seed = flat(map(
  combine({
    constantSeed: value(constantSeed), // wrap inside value so it won't be resolved
    randomSeed: value(randomSeed), // wrap inside value so it won't be resolved
  }),
  async ({ 
    constantSeed,
    randomSeed
  }) => condition ? constantSeed : randomSeed
))

And flatMap does exactly so

const seed = flatMap(
  combine({
    constantSeed: value(constantSeed), // wrap inside value so it won't be resolved
    randomSeed: value(randomSeed), // wrap inside value so it won't be resolved
  }),
  async ({ 
    constantSeed,
    randomSeed
  }) => condition ? constantSeed : randomSeed
)

Testing

Main purpose of submodule is to make the code

  • side-effect free (app'll be faster to start)
  • testing made easy
  • testing should be fast, and easy, mock free and can run in parallel

Scope is the key in this testing technique

There are certain common testing techniques

Assume value in the change to simluate different testing situations

For example

function tomorrow() {
  /** implementation */
}

This function is likely rely on new Date() to implement. This is a global object and by mocking the global object, the test will not be able to run in parallel and depending on test framework mocking to be able to test.

Rewrite the code into

const newDateFn = value(() => new Date())
const tomorrowFn = map(newDateFn, (newDateFn) => {
  /** Implementation **/
})

The implementation is mostly the same, now how to test?

// use ResolveValue to force value of dependency

const scope = createScope()
scope.resolveValue(newDateFn, value(() => /* mock date*/))

const r = await scope.resolve(torrowFn)
r() // <-- day after the mock date

更新履歴

Changelog

All notable changes to this project will be documented in this file. See commit-and-tag-version for commit guidelines.

12.5.1 (2025-01-17)

Bug Fixes

  • changed name of get to value and getState to state (d9e9a82)

12.5.0 (2025-01-17)

Features

  • added get and getState to subscribable and controllerBase (7168963)

12.4.1 (2025-01-16)

Bug Fixes

  • Emission type was way too wide (d083e1a)

12.4.0 (2025-01-16)

Features

  • expose observables.createState and provideState (87cb88c)

12.3.2 (2025-01-16)

Bug Fixes

12.3.1 (2025-01-15)

Bug Fixes

12.3.0 (2025-01-15)

Features

  • added operators.withLatestFrom (406a1c6)
  • expose lastValue from the stream (406a1c6)

12.2.0 (2025-01-14)

Features

  • improve tap operator DX (6905a30)

12.1.2 (2025-01-14)

Bug Fixes

  • pushObservable should emit initial value (131231b)
  • removed types/react optional dependency (2d2d925)

12.1.1 (2025-01-14)

Bug Fixes

  • ControllableObservable to be readonly (52af2ae)

12.1.0 (2025-01-14)

Features

  • renamed usePushObservable to useControllableObservable, added ControllableObservable type (e1442a5)

Bug Fixes

  • cleanup package.json dependency list (2845131)

12.0.1 (2025-01-14)

Bug Fixes

  • react components to render infinitely (0e3c47c)

12.0.0 (2025-01-13)

⚠ BREAKING CHANGES

  • removed observable, replaced by reactive concepts from reactivex
  • added Scope to the 2nd parameter of constructor, (1st in case of provide

Features

  • added Scope to the 2nd parameter of constructor, (1st in case of provide (a2f9cbb)
  • applied rx to react (a2f9cbb)
  • removed observable, replaced by reactive concepts from reactivex (a2f9cbb)

Bug Fixes

  • corrected test run for cases of combining (99ba1d9)

11.5.1 (2025-01-07)

Bug Fixes

  • corrected transformObservable signature (3f46571)

11.5.0 (2025-01-06)

Features

  • minor - exposed transformObservable (8c3f544)

11.4.0 (2025-01-06)

Features

  • merged submodule-react back to the core, under /react (a60f5f1)

Bug Fixes

  • refined certain observables API (a60f5f1)

11.3.1 (2025-01-03)

Bug Fixes

  • also apply options to Executor (6e4a0bf)

11.3.0 (2025-01-03)

Features

  • added onMount options to observable (99ef58b)

11.2.0 (2025-01-03)

Features

  • added combineObservables (ca7fe58)

11.1.0 (2024-12-31)

Features

  • changed the pipe API, let the piped stream to be editable (e166738)

11.0.0 (2024-12-30)

⚠ BREAKING CHANGES

  • remove depreacted utilities, removed defaults, presets and executes which are never used

Features

  • added preferredScope to organize where an executor can be resolved (7140902)

Miscellaneous Chores

  • remove depreacted utilities, removed defaults, presets and executes which are never used (7140902)

10.2.1 (2024-12-26)

Bug Fixes

  • fixed snapshot issues on sub-path (e83186e)

10.2.0 (2024-12-26)

Features

  • simplified the observables API again (d66bef7)

10.1.1 (2024-12-24)

Bug Fixes

  • reorder checking order to make the lib more reliable (8146580)

10.1.0 (2024-12-24)

Features

  • changed the obserfvable API again (5259ac3)

10.0.0 (2024-12-24)

⚠ BREAKING CHANGES

  • rewrote observable to simplify those APIs

Features

  • rewrote observable to simplify those APIs (dd28ef6)

9.3.3 (2024-12-19)

Bug Fixes

  • Prettify type was too aggressive (24de777)

9.3.2 (2024-12-19)

Bug Fixes

  • corrected pipe behavior (9374764)

9.3.1 (2024-12-19)

Bug Fixes

9.3.0 (2024-12-19)

Features

  • made pipe on observable to be more meaningful (8b2bf67)

9.2.2 (2024-12-19)

Bug Fixes

  • corrected cleanup behavior (7e54d21)

9.2.1 (2024-12-19)

Bug Fixes

  • exposed more types for observables (5f0c935)

9.2.0 (2024-12-19)

Features

  • added observable utilities under /observables (ce277ba)
  • updated observable and pipe API (ce277ba)

9.1.1 (2024-12-16)

Features

Miscellaneous Chores

9.1.0 (2024-12-16)

Features

  • added pipe utility (0ef68fb)
  • restructure slice option for observable (0ef68fb)

9.0.0 (2024-12-13)

⚠ BREAKING CHANGES

  • drop observable experimental API, replace with observable utility

Features

  • drop observable experimental API, replace with observable utility (381dc57)

8.3.1 (2024-12-13)

Bug Fixes

  • corrected Promise return behavior, make sure the promise is the same (59ab52d)

8.3.0 (2024-12-12)

Features

8.2.1 (2024-12-11)

Bug Fixes

  • minor change to make observe api less verbose (7adcba7)

8.2.0 (2024-12-11)

Features

  • simplified observable API (736d216)

Bug Fixes

8.1.0 (2024-12-10)

Features

  • added controller API so stream can also be controlled from outside (eed0547)

8.0.0 (2024-12-10)

Features

  • added experimental publisher (aa47ec4)
  • added more capability to filter (aa47ec4)
  • added more meaningful id to each executor (aa47ec4)
  • added update API to trigger changes (aa47ec4)
  • move resolve logic to scope (instead of individual executor) (aa47ec4)

Bug Fixes

  • use Map instead of WeakMap (ee29eba)

Tests

7.6.0 (2024-11-13)

Features

7.5.8 (2024-11-08)

Features

  • added experimental preset* API (bee39e0)

Miscellaneous Chores

7.5.7 (2024-11-07)

Bug Fixes

Miscellaneous Chores

7.5.6 (2024-11-07)

Bug Fixes

  • correct the case handling function as key (ce0ac36)

7.5.5 (2024-11-07)

Bug Fixes

  • corrected keybuilder behavior (82e322e)

7.5.4 (2024-11-07)

Bug Fixes

  • loosen type for createFamily (05262dd)

7.5.3 (2024-11-07)

Bug Fixes

  • improved createFamily typing (6bdd907)

7.5.2 (2024-11-07)

Bug Fixes

  • corrected types for group (5cc0565)

7.5.1 (2024-11-07)

Features

  • refined options for createFamily, with cacheControl (20d2a53)

7.5.0 (2024-11-06)

Features

  • added createFamily API to address building actor-like, multiple instances (a2cb489)

7.4.2 (2024-11-05)

Bug Fixes

  • remove codemod so the path became normal (c720f38)

7.4.1 (2024-11-05)

Features

  • added executor#separate to combine to retrieve the original pre combined executor (c5d457c)

Bug Fixes

  • use jscodeshift instead of typescript on its own for codemod (141830b)

7.4.0 (2024-11-01)

Features

  • added codemod to convert create to provide and map (e6710c0)

7.3.0 (2024-10-29)

Features

  • added safeRun API (to replace execute) (e081526)

7.2.0 (2024-10-28)

Features

  • added safeResolve API (059d6d7)
  • make combine compatible with array as well (8c4685a)

Miscellaneous Chores

7.1.2 (2024-10-28)

Miscellaneous Chores

  • change dependency type, this package has no dependency (f978d6c)

7.1.1 (2024-10-28)

⚠ BREAKING CHANGES

  • prepare for next version
  • clean up from API
  • simplify api surface
  • completely oversimplified API
  • made core way simpler

Features

  • add overriding (676a47b)
  • added booting and shutdown utilities (766407c)
  • added compose api (9ca937c)
  • added createFactory, an atom to have better authoring (b0e2018)
  • added make API (97e968d)
  • added onError and onExecute (08849c3)
  • added prepare function (938b719)
  • added prepare to from result (59a5af9)
  • added prepareExecutable to support serveless environment (075e05d)
  • added ProvideOption to all operations (ea6e1cb)
  • added providerOption to staged (d1376ae)
  • added proxify package (18882da)
  • added receipes package to cover common patterns (00fcf2d)
  • added runing mode, added hijack to test dependencies (937c074)
  • api update, executor can also be used as initArgs (0d2b698)
  • bring back the template, remove stage and unstage (b6b2cf8)
  • bundle ts-toolbelt (10cb739)
  • change API to get and execute (from execute like get) (d6669b4)
  • change for 2.1 (0f54ba4)
  • clean up from API (c97e506)
  • completely oversimplified API (5a4d721)
  • finalized instrument api (5807e1e)
  • made core way simpler (c128d43)
  • magic function can accept variadic arguments (9432bf5)
  • make prepare to take variadic arguments (7d242ab)
  • moved to bun, added few fancy API (a73b951)
  • prepare flow release (8729869)
  • prepare for next version (6ea6394)
  • prepare to release new instrument api (854d15b)
  • restructure stage apis (db70c7d)
  • simplify api surface (9e76b20)
  • submodule: added combineScope API (316825e)

Bug Fixes

Miscellaneous Chores

7.1.0 (2024-10-25)

Features

  • submodule: added combineScope API (316825e)

6.1.0 (2024-10-04)

Features

  • added createFactory, an atom to have better authoring (b0e2018)

6.0.0 (2024-08-27)

6.0.0-rc.8 (2024-06-14)

Features

  • added flat api to turn Executor<Executor<T>> to Executor<T> (38846d8)

6.0.0-rc.7 (2024-06-13)

6.0.0-rc.6 (2024-06-13)

Features

  • added helpers module (migrated from previous meta) (2bf2847)
  • added substituion api (a1d3df3)

6.0.0-rc.5 (2024-06-11)

Features

  • applied EDOE to all API (0406290)

6.0.0-rc.4 (2024-06-10)

6.0.0-rc.3 (2024-06-10)

Features

  • can use object instead of combine (e90b503)

6.0.0-rc.2 (2024-06-10)

6.0.0-rc.1 (2024-06-06)

6.0.0-rc.0 (2024-06-06)

Features

  • added a lot of new api for metas (69c4d21)
  • added scope and its api (1937024)
  • oversimplified submodule api (b8639d7)

5.2.0 (2024-01-04)

5.1.4 (2024-01-04)

Features

5.1.3 (2023-10-31)

5.1.2 (2023-10-31)

5.1.1 (2023-10-30)

5.1.0 (2023-10-30)

Features

  • added onError and onExecute (08849c3)

5.0.0 (2023-10-18)

⚠ BREAKING CHANGES

  • prepare for next version

Features

  • added booting and shutdown utilities (766407c)
  • added providerOption to staged (d1376ae)
  • bring back the template, remove stage and unstage (b6b2cf8)
  • moved to bun, added few fancy API (a73b951)
  • prepare for next version (6ea6394)
  • restructure stage apis (db70c7d)

Bug Fixes

  • throw error on initialization failure (af0462c)

5.0.0-rc.1 (2023-09-17)

Features

  • bring back the template, remove stage and unstage (b6b2cf8)

5.0.0-rc.0 (2023-09-10)

⚠ BREAKING CHANGES

  • prepare for next version
  • clean up from API
  • simplify api surface
  • completely oversimplified API
  • made core way simpler
  • prepare for a very big change
  • added tsup to bundle, added dev command, added build command
  • changed API, completly

Features

  • add more examples (420f669)
  • add overriding (676a47b)
  • added booting and shutdown utilities (766407c)
  • added commands dir, for extension (1734917)
  • added commands extension without commander (6318ce0)
  • added compose api (9ca937c)
  • added createCaller API to call from outside (6ccb4c0)
  • added createClient, access instance out of context (07f3ca3)
  • added deno checking (5174657)
  • added few more APIs, SPIs (caa6f84)
  • added more defaults (59cf533)
  • added more generate-friendly functions (591f2a6)
  • added prepare function (938b719)
  • added prepare to from result (59a5af9)
  • added prepareExecutable to support serveless environment (075e05d)
  • added ProvideOption to all operations (ea6e1cb)
  • added providerOption to staged (d1376ae)
  • added runing mode, added hijack to test dependencies (937c074)
  • added submodule builder (c2933c4)
  • added submoduleArg to createCommands to create generate method (c467ac2)
  • added tsup to bundle, added dev command, added build command (81b3c65)
  • adding sensible defaults (9414e11)
  • api update, executor can also be used as initArgs (0d2b698)
  • auto carry shape of routeModule to the router, less code for createRoute (5ee2cb8)
  • bundle ts-toolbelt (10cb739)
  • by default, subcommand will just call the default function of the corresponding route (10fd127)
  • change API to get and execute (from execute like get) (d6669b4)
  • change for 2.1 (0f54ba4)
  • changed API, completly (f99388c)
  • clean up from API (c97e506)
  • completely oversimplified API (5a4d721)
  • finalized instrument api (5807e1e)
  • launch (b3261b4)
  • made aop (2e29d68)
  • made cli available as submodule (b73fda7)
  • made core way simpler (c128d43)
  • magic function can accept variadic arguments (9432bf5)
  • make prepare to take variadic arguments (7d242ab)
  • moved to bun, added few fancy API (a73b951)
  • prepare for a very big change (e1c837f)
  • prepare for next version (6ea6394)
  • prepare to release new instrument api (854d15b)
  • restraint api version of opentelemetry (9981a02)
  • restructure stage apis (db70c7d)
  • simplify api surface (9e76b20)
  • update type API (87dbf92)
  • upgrade types API (c33e2e2)

Bug Fixes

4.6.2 (2023-06-21)

Features

  • make prepare to take variadic arguments (7d242ab)

4.6.1 (2023-06-19)

Features

  • add overriding (676a47b)
  • added ProvideOption to all operations (ea6e1cb)

4.6.0 (2023-06-16)

⚠ BREAKING CHANGES

  • clean up from API

Features

  • clean up from API (c97e506)
  • finalized instrument api (5807e1e)
  • prepare to release new instrument api (854d15b)

4.5.0 (2023-06-13)

Features

  • finalized instrument api (5807e1e)
  • prepare to release new instrument api (854d15b)

4.4.2 (2023-06-09)

4.4.1 (2023-06-09)

4.4.0 (2023-06-06)

Features

  • magic function can accept variadic arguments (9432bf5)

4.3.0 (2023-06-06)

Features

  • added prepare function (938b719)
  • added prepare to from result (59a5af9)

4.2.1 (2023-06-05)

Bug Fixes

4.2.0 (2023-06-05)

Features

  • change API to get and execute (from execute like get) (d6669b4)

4.1.0 (2023-05-31)

Features

  • added runing mode, added hijack to test dependencies (937c074)

4.0.0 (2023-05-31)

⚠ BREAKING CHANGES

  • simplify api surface

Features

3.2.0 (2023-05-09)

Features

  • api update, executor can also be used as initArgs (0d2b698)

3.1.0 (2023-05-08)

Features

3.0.0 (2023-05-08)

⚠ BREAKING CHANGES

  • completely oversimplified API

Features

  • completely oversimplified API (5a4d721)

2.1.0 (2023-05-06)

Features

  • added instrument option, each function can be wrapped and instrumented
  • initArgs can be added using an async function, that helps with function composition
  • added config() and services() to retrieve config and services directly
  • change for 2.1 (0f54ba4)

2.0.0 (2023-05-05)

⚠ BREAKING CHANGES

  • made core way simpler

Features

1.1.1 (2023-05-04)

Features

1.1.0 (2023-05-04)

⚠ BREAKING CHANGES

  • prepare for a very big change
  • added tsup to bundle, added dev command, added build command
  • changed API, completly

Features

  • add more examples (420f669)
  • added commands dir, for extension (1734917)
  • added commands extension without commander (6318ce0)
  • added createCaller API to call from outside (6ccb4c0)
  • added createClient, access instance out of context (07f3ca3)
  • added deno checking (5174657)
  • added few more APIs, SPIs (caa6f84)
  • added more defaults (59cf533)
  • added more generate-friendly functions (591f2a6)
  • added prepareExecutable to support serveless environment (075e05d)
  • added submodule builder (c2933c4)
  • added submoduleArg to createCommands to create generate method (c467ac2)
  • added tsup to bundle, added dev command, added build command (81b3c65)
  • adding sensible defaults (9414e11)
  • auto carry shape of routeModule to the router, less code for createRoute (5ee2cb8)
  • by default, subcommand will just call the default function of the corresponding route (10fd127)
  • changed API, completly (f99388c)
  • launch (b3261b4)
  • made aop (2e29d68)
  • made cli available as submodule (b73fda7)
  • prepare for a very big change (e1c837f)
  • restraint api version of opentelemetry (9981a02)
  • update type API (87dbf92)
  • upgrade types API (c33e2e2)

Bug Fixes

1.0.4 (2023-04-30)

Bug Fixes

1.0.3 (2023-04-30)

1.0.2 (2023-04-30)

1.0.1 (2023-04-30)