Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 3th party libraries to expand Teaful with extras #50

Merged
merged 4 commits into from
Nov 24, 2021

Conversation

aralroca
Copy link
Collaborator

@aralroca aralroca commented Nov 23, 2021

#38

Allow 3th party libraries to expand Teaful with extras. Required for teaful-devtools, etc.

Example devtools

Example of future devtools bridge:

https://codesandbox.io/s/teaful-canary-y72vv?file=/src/store.js

import createStore from 'teaful'

export default function initDevtools() {
  window.__TEAFUL_DEVTOOLS__ = [];

  // Ext = Extra / Extension
  // Function that is executed in each createStore, it allows:
  // - Access to { getStore, useStore, withStore }
  // - Subscriptions: subscribe, notify, unsubscribe.
  // - Return an object (optional) with new store properties.
  createStore.ext(({ getStore }, subscription) => {
    let index = window.__TEAFUL_DEVTOOLS__.push(getStore) - 1;

    // s = subscribe (minified by Teaful)
    // n = notify (minified by Teaful)
    // u = unsubscribe (minified by Teaful)
    subscription.s(".", ({ store, prevStore }) => {
      window.postMessage(
        { source: "teaful-devtools", store, prevStore, index },
        "*"
      );
    });

   // Here is not necessary but is possible to return an object with
   // new things: 
   // return { getExample }
   //
   // These will be available in each createStore with the other:
   // const { getExample, useStore } = createStore()
  });
}

And then to use it:

import createStore from 'teaful'
import initDevtools from 'teaful-devtools' // Not definive dependency

if (process.env.NODE_ENV !== 'production') initDevtools()

const { useStore } = createStore({})
// ...

@aralroca aralroca requested a review from danielart November 23, 2021 17:50
@aralroca aralroca changed the title Add extras Allow 3th party libraries to expand Teaful with extras Nov 23, 2021
@aralroca aralroca self-assigned this Nov 23, 2021
@aralroca
Copy link
Collaborator Author

Prereleased in 0.9.0-canary.1

@aralroca aralroca merged commit bc6a297 into master Nov 24, 2021
@aralroca aralroca deleted the aral-add-extras branch November 24, 2021 16:46
@aralroca aralroca added this to the Before 1.0.0 milestone Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant