~evanj/wigly

55fb3190e774cd5ec3bf650df6079697150d69f6 — Evan M Jones 5 years ago 51dc358
await fix for effect cleanup
5 files changed, 26 insertions(+), 17 deletions(-)

M package.json
M src/main.js
M src/wigly/constants.js
M src/wigly/transformer.js
M test/main.js
M package.json => package.json +1 -1
@@ 1,6 1,6 @@
{
  "name": "wigly",
  "version": "0.5.7",
  "version": "0.5.8",
  "main": "dist/wigly.es5.js",
  "scripts": {
    "start": "npm run start:clean && npm run start:gcc && npm run start:gzip",

M src/main.js => src/main.js +3 -0
@@ 5,3 5,6 @@ if (typeof module !== "undefined") {
} else {
  window["wigly"] = wigly;
}

// export * from "./wigly/wigly";
// export default wigly;

M src/wigly/constants.js => src/wigly/constants.js +7 -9
@@ 25,16 25,14 @@ export let runEffects = (el, self) => {
  for (let key in self.effects) {
    let { prev, args, f, cleanup } = self.effects[key];
    if (args && f && (typeof prev === "undefined" || args.length === 0 || prev.join() !== args.join())) {
      if (cleanup) cleanup();

      cleanup = f(el);
      if (cleanup && cleanup.then) {
        cleanup.then(cleanup => {
          self.effects[key] = { prev: args, cleanup };
        });
      } else {
        self.effects[key] = { prev: args, cleanup };
      if (cleanup) {
        if (cleanup.then) {
          cleanup.then(f => f && f());
        } else {
          cleanup();
        }
      }
      self.effects[key] = { prev: args, cleanup: f(el) };
    }
  }
};

M src/wigly/transformer.js => src/wigly/transformer.js +15 -4
@@ 24,6 24,7 @@ export let transformer = (spec, getEnv, giveEnv, giveVDOM, updateVDOM) => {

    /** @type {ComponentContext} */
    let self = {
      f,
      states: [],
      effects: [],
      children: {},


@@ 82,14 83,18 @@ export let transformer = (spec, getEnv, giveEnv, giveVDOM, updateVDOM) => {
              props: {
                ...vdom.props,
                oncreate: el => {
                  if (oncreate) oncreate(el);
                  if (oncreate) {
                    oncreate(el);
                  }
                  defer(() => {
                    runEffects(el, self);
                    giveEnv(f, props.key, self);
                  });
                },
                onupdate: el => {
                  if (onupdate) onupdate(el);
                  if (onupdate) {
                    onupdate(el);
                  }
                  defer(() => {
                    updateVDOM(f, props.key, lastvdom);
                    runEffects(el, self);


@@ 97,10 102,16 @@ export let transformer = (spec, getEnv, giveEnv, giveVDOM, updateVDOM) => {
                  });
                },
                ondestroy: () => {
                  if (ondestroy) ondestroy();
                  if (ondestroy) {
                    ondestroy();
                  }
                  for (let effect of self.effects) {
                    if (effect && effect.cleanup) {
                      effect.cleanup();
                      if (effect.cleanup.then) {
                        effect.cleanup.then(f => f && f());
                      } else {
                        effect.cleanup();
                      }
                    }
                  }
                  giveEnv(f, props.key, { iter: 0 }); // reset state

M test/main.js => test/main.js +0 -3
@@ 3,9 3,6 @@ require("browser-env")();
let test = require("ava");
let { h, render, state, effect } = require("../");

// console.log(require("../"));
// process.exit(0);

let sleep = t => new Promise(r => setTimeout(r, t));

test("'Hello, World'", t => {