Skip to content

Commit

Permalink
fix test bug, and add lib
Browse files Browse the repository at this point in the history
xiangxn committed Dec 30, 2021
1 parent d426acb commit ffa88be
Showing 11 changed files with 1,216 additions and 3 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ coverage
node_modules
npm-debug.log
test/browser/tests.js
lib
utils/*
flux.js
flux-build.js
82 changes: 82 additions & 0 deletions lib/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = makeAction;

var _isPromise = _interopRequireDefault(require("is-promise"));

var fn = _interopRequireWildcard(require("../functions"));

var utils = _interopRequireWildcard(require("../utils/AltUtils"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function makeAction(alt, namespace, name, implementation, obj) {
var id = utils.uid(alt._actionsRegistry, "".concat(namespace, ".").concat(name));
alt._actionsRegistry[id] = 1;
var data = {
id: id,
namespace: namespace,
name: name
};

var dispatch = function dispatch(payload) {
return alt.dispatch(id, payload, data);
}; // the action itself


var action = function action() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var invocationResult = implementation.apply(obj, args);
var actionResult = invocationResult; // async functions that return promises should not be dispatched

if (invocationResult !== undefined && !(0, _isPromise["default"])(invocationResult)) {
if (fn.isFunction(invocationResult)) {
// inner function result should be returned as an action result
actionResult = invocationResult(dispatch, alt);
} else {
dispatch(invocationResult);
}
}

if (invocationResult === undefined) {
utils.warn('An action was called but nothing was dispatched');
}

return actionResult;
};

action.defer = function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}

return setTimeout(function () {
return action.apply(null, args);
});
};

action.id = id;
action.data = data; // ensure each reference is unique in the namespace

var container = alt.actions[namespace];
var namespaceId = utils.uid(container, name);
container[namespaceId] = action; // generate a constant

var constant = utils.formatAsConstant(namespaceId);
container[constant] = id;
return action;
}

module.exports = exports.default;
39 changes: 39 additions & 0 deletions lib/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.assign = assign;
exports.eachObject = eachObject;
exports.isFunction = void 0;
exports.isMutableObject = isMutableObject;

var isFunction = function isFunction(x) {
return typeof x === 'function';
};

exports.isFunction = isFunction;

function isMutableObject(target) {
var Ctor = target.constructor;
return !!target && Object.prototype.toString.call(target) === '[object Object]' && isFunction(Ctor) && !Object.isFrozen(target) && (Ctor instanceof Ctor || target.type === 'AltStore');
}

function eachObject(f, o) {
o.forEach(function (from) {
Object.keys(Object(from)).forEach(function (key) {
f(key, from[key]);
});
});
}

function assign(target) {
for (var _len = arguments.length, source = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
source[_key - 1] = arguments[_key];
}

eachObject(function (key, value) {
return target[key] = value;
}, source);
return target;
}
Loading

0 comments on commit ffa88be

Please sign in to comment.