Skip to content

Commit

Permalink
update default event type to be listen.
Browse files Browse the repository at this point in the history
  • Loading branch information
wildone committed Oct 7, 2024
1 parent 6eee4f9 commit 3253b21
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ Typerefinery.Page.Events = Typerefinery.Page.Events || {};
const configData = ns.parseJson(config) || "";
//if topic not set use component id as topic
const topicName = topic || defaultTopic;
// if type is not defined then its emitted
let typeName = type || eventNs.EVENT_TYPE_LISTEN; //default is listen
// if type is not defined then its listen event
let typeName = type || eventNs.EVENT_TYPE_LISTEN || "custom";

//custom name takes precidence over name, this will be raised as event name
let eventName = nameCustom || name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ window.Typerefinery.Page.Events = Typerefinery.Page.Events || {};
const { topic, type, name, nameCustom, action, config } = event;
//if topic not set use component id as topic
const topicName = topic || defaultTopic;
// if type is not defined then its emitted
let typeName = type || eventNs.EVENT_TYPE_LISTEN;
// if type is not defined then its listen event
let typeName = type || eventNs.EVENT_TYPE_LISTEN || "custom";

let eventName = nameCustom || name;
console.group(action + " " + eventName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ window.Typerefinery.Page.Events = Typerefinery.Page.Events || {};
const { topic, type, name, nameCustom, action, config } = event;
//if topic not set use component id as topic
const topicName = topic || defaultTopic;
// if type is not defined then its emitted
// if type is not defined then its listen event
let typeName = type || eventNs.EVENT_TYPE_LISTEN || "custom";

let eventName = nameCustom || name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ window.Typerefinery.Page.Files = Typerefinery.Page.Files || {};
};

// submit the request to the server
ns.submit = async ($component, componentConfig, url, method, payloadType, body, successCallback = () => { }, errorCallback = () => { }) => {
ns.submit = async ($component, componentConfig, url, method, payloadType, body, successCallback = ($component, componentConfig, payload) => { }, errorCallback = ($component, componentConfig, payload) => { }) => {
console.group("submit");
console.log([url, method, payloadType, body])
let controller = new AbortController();
Expand Down Expand Up @@ -516,9 +516,10 @@ window.Typerefinery.Page.Files = Typerefinery.Page.Files || {};
};

ns.addEventListener = ($component, componentConfig) => {
console.group('addEventListener form');
const { events, id } = componentConfig;
const defaultTopic = id;

console.group('addEventListener ' + id);

console.log(["config", events, id, defaultTopic]);

Expand All @@ -528,6 +529,7 @@ window.Typerefinery.Page.Files = Typerefinery.Page.Files || {};
events.forEach(event => {
const { topic, type, name, nameCustom, action, config} = event;


//if type unset then log warning
if (!type) {
console.warn("type is unset for event will default to emit", event);
Expand All @@ -545,11 +547,16 @@ window.Typerefinery.Page.Files = Typerefinery.Page.Files || {};

//if topic not set use component id as topic
const topicName = topic || defaultTopic;
// if type is not defined then its emitted
let typeName = type || eventNs.EVENT_TYPE_EMIT;
// if type is not defined then its listen event
let typeName = type || eventNs.EVENT_TYPE_LISTEN || "custom";
//custom name takes precidence over name, this will be raised as event name
let eventName = nameCustom || name;

console.group(action + " " + eventName);

console.log(["event config", topic, type, name, nameCustom, action]);
console.log(["event to register", topicName, typeName, eventName, action]);

console.log(["registerEventActionMapping", JSON.stringify(ns.eventMap), topicName, typeName, action, eventName]);
eventNs.registerEventActionMapping(ns.eventMap, id, topicName, typeName, action, eventName, config);
console.log(["registerEventActionMapping", JSON.stringify(ns.eventMap)]);
Expand All @@ -561,16 +568,18 @@ window.Typerefinery.Page.Files = Typerefinery.Page.Files || {};
console.info("event type is emit, do nothing");
} else {
//listen register the event and listent for specific event on topic
console.log(["registerEvents", topicName, eventName]);
console.log(["register event listen", topicName, eventName]);
eventNs.registerEvents(topicName, (data) => {
console.log(["registerEvents consumeIncomingEvent", data, eventName]);
// check make sure the event is for this event
if (data.type === eventName) {
console.log(["registerEvents match calling update", action, data]);
ns.update($component, action, data);
ns.handleEventAction($component, action, data);
}
});
}

console.groupEnd();
});
}

Expand Down Expand Up @@ -631,24 +640,29 @@ window.Typerefinery.Page.Files = Typerefinery.Page.Files || {};
console.groupEnd();
}


/**
*
* @param {*} $component
* @param {*} actionName can be either global action name or component action name
* @param {*} data
* handle events for the form component
* @param {*} $component component instance
* @param {*} action component action
* @param {*} data data to be passed to the action
*/
ns.update = ($component, actionName, data) => {
console.log(["form update", $component, actionName, data]);
//load data into form if action is FORM_LOAD or EVENT_READ_ACTION
if (ns.ACTIONS[actionName] == "FORM_LOAD" || actionName === eventNs.EVENTS.EVENT_READ_ACTION) {
ns.loadData($component, data);
} else if (ns.ACTIONS[actionName] == "FORM_SUBMIT" || actionName == eventNs.EVENTS.EVENT_SUBMIT_ACTION) {
//submit form if action is FORM_SUBMIT or EVENT_SUBMIT_ACTION
ns.formSubmitHandler($component);
} else {
//log error if action is not supported
console.error(["unsupported action", actionName]);
}
ns.handleEventAction = ($component, action, data) => {
console.group('handleEvent');
console.log(["handleEvent", $component, action, data]);

//load data into form if action is FORM_LOAD or EVENT_READ_ACTION
if (ns.ACTIONS[action] == "FORM_LOAD" || action === eventNs.EVENTS.EVENT_READ_ACTION) {
ns.loadData($component, data);
} else if (ns.ACTIONS[action] == "FORM_SUBMIT" || action == eventNs.EVENTS.EVENT_SUBMIT_ACTION) {
//submit form if action is FORM_SUBMIT or EVENT_SUBMIT_ACTION
ns.formSubmitHandler($component);
} else {
//log error if action is not supported
console.error(["unsupported action", action]);
}

console.groupEnd();
}

ns.init = ($component) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ window.Typerefinery.Page.Events = Typerefinery.Page.Events || {};
const { topic, type, name, nameCustom, action, config } = event;
//if topic not set use component id as topic
const topicName = topic || defaultTopic;
// if type is not defined then its emitted
// if type is not defined then its listen event
let typeName = type || eventNs.EVENT_TYPE_LISTEN || "custom";

let eventName = nameCustom || name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ window.Typerefinery.Page.Events = Typerefinery.Page.Events || {};
const { topic, type, name, nameCustom, action, config } = event;
//if topic not set use component id as topic
const topicName = topic || defaultTopic;
// if type is not defined then its emitted
// if type is not defined then its listen event
let typeName = type || eventNs.EVENT_TYPE_LISTEN || "custom";

let eventName = nameCustom || name;
Expand Down

0 comments on commit 3253b21

Please sign in to comment.